<?php
include "../lib/DAL/DmMonHoc.php";
class DmMonHocBLL{
    function __construct(){
        
    }
    
    function __destruct(){
        
    }
    
    function setDmMonHocBLL($row){
        $monhoc= new DmMonHoc();
        $monhoc->setDmMonHoc($row['madm_mh'],$row['ten_mh'],$row['ten_vt_mh']);
        return $monhoc;
         
    }
	public function getDmMonHoc($ma_monhoc){
        $sql = "select * from dm_monhoc where madm_mh ='".$ma_monhoc."'";        
        $row = mysql_fetch_array(mysql_query($sql));
       return $this->setDmMonHocBLL($row);
        
    }
    public function getListDmMonHoc(){
        $result = mysql_query("select * from dm_monhoc");
        $arr_monhoc= array();
        while($row = mysql_fetch_array($result)){
            $arr_monhoc[] =  $this->setDmMonHocBLL($row);
        }
        return $arr_monhoc;
    }
    public function getPages($where,$offset,$limit){
        $result = mysql_query("SELECT * 
                            FROM dm_monhoc 
                            $where 
                            LIMIT $offset, $limit");
        $arr_monhoc= array();
        
        while($row = mysql_fetch_array($result)){
            $arr_monhoc[] =  $this->setDmMonHocBLL($row);
        }
        return $arr_monhoc;
    }
    
    public function Insertmonhoc($dmmh){
            $sql ="INSERT INTO dm_monhoc(madm_mh,ten_mh,ten_vt_mh) 
                VALUES ('".$dmmh->getmadm_mh()."' 
                        ,'".$dmmh->getten_mh()."'
                        ,'".$dmmh->getten_vt_mh()."'
                )";
            return $kq = mysql_query($sql) or die (mysql_error());
     }
     public function getItem($madm_mh){
        $sql="select * from dm_monhoc where madm_mh='".$madm_mh."'";
        $ex=mysql_query($sql);
        if (mysql_num_rows($ex)){
            $kq =mysql_fetch_array($ex);
            }
        return $kq;
    }
    public function getMonCDK($ma_ctdt){
        $sql =" SELECT M.madm_mh, M.ten_mh, M.ten_vt_mh 
                FROM  dm_monhoc AS M 
                HAVING madm_mh NOT IN ( SELECT ma_monhoc 
                                        FROM tbl_mon_ctdt 
                                        WHERE ma_ctdt like '$ma_ctdt'
                                    )";
        $rs = mysql_query($sql) or die(mysql_error());
        $arr_ctdt = array();
        while($row = mysql_fetch_array($rs)){
            $arr_ctdt[] = $this->setDmMonHocBLL($row);
        }
        return $arr_ctdt;
    }
    /////// sửa /////////
    public function updatemonhoc($monhoc){
        $sql="UPDATE dm_monhoc
            SET
            ten_mh = '".$monhoc->getten_mh()."',
            ten_vt_mh = '".$monhoc->getten_vt_mh()."'   
            WHERE madm_mh ='".$monhoc->getmadm_mh()."'";
         return $kq = mysql_query($sql) or die (mysql_error());
       
    }
    ///////// xóa ///////////
    public function deletedmmonhoc($madm_mh){
        $sql="delete from dm_monhoc where madm_mh = '".$madm_mh."'";
         $kq = mysql_query($sql) or die (mysql_error());
       return $kq;
    }
    //////// xóa tất cả ///////
    public function deleteAll(){
        $sql="DELETE FROM dm_monhoc";
        $kq = mysql_query($sql) or die (mysql_error());
        return $kq;
    }
    // Phân trang ////////////////
    function paging($totalRecords,$limit,$url,$css){
		$totalPage=ceil($totalRecords/$limit);
		$pager='';
		for($i=1;$i<=$totalPage;$i++){
            $pager.="<a href='$url&p=$i' class='$css'>$i</a>";
		}
		return $pager;
	}
	function buildSearchQueryLike($keyword){    
		$dieukien="";   
		$qse=" OR ";
		$arr = tachChuoi($keyword);	
		foreach($arr as $kw){						
			$dieukien .=" madm_mh LIKE '%$kw%' OR ten_mh LIKE '%$kw%'";			
			$dieukien .= $qse;	
							
		}
		$cstr = strlen($qse);
		$dieukien = substr($dieukien,0,-$cstr);		
		return $dieukien;	
	}
    function timkiem($where){
        $sql="SELECT madm_mh, ten_mh, ten_vt_mh FROM dm_monhoc $where";
        $ex=mysql_query($sql);
        if($ex){
            while($row=mysql_fetch_array($ex)){
                $dstk[]=$this->setDmMonHocBLL($row);
            }
        }
        return $dstk;
    }


 }
?>